package com.huabo.monitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hbfk.util.JsonBean;
import com.hbfk.util.PageInfo;
import com.hbfk.util.ResponseFormat;
import com.huabo.monitor.oracle.entity.TblAssesselement;
import com.huabo.monitor.oracle.entity.TblAssesstemple;
import com.huabo.monitor.oracle.entity.TblOrganization;
import com.huabo.monitor.oracle.mapper.OrganizationServiceMapper;
import com.huabo.monitor.oracle.mapper.TblAssesstempleMapper;
import com.huabo.monitor.service.TblAssesstempleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Service("TblAssesstempleService")
public class TblAssesstempleServiceImpl implements TblAssesstempleService {

    @Resource
    private TblAssesstempleMapper tblAssesstempleMapper;

    @Resource
    private OrganizationServiceMapper organizationServiceMapper;


    @Override
    public JsonBean findByPageInfo(Integer pageNumber, Integer pageSize, TblAssesstemple tblAssesstemple) {
        if (pageNumber == null || pageNumber <= 0){
            pageNumber = 1;
        }
        if (pageSize <= 0 || pageSize >15){
            pageSize = 15;
        }

        PageInfo<TblAssesstemple> pageInfo = new PageInfo<>();
        pageInfo.setCurrentPage(pageNumber);
        pageInfo.setPageSize(pageSize);
        pageInfo.setCondition(tblAssesstemple);

        List<TblAssesstemple> list = tblAssesstempleMapper.findByPageInfo(pageInfo);
        Integer count = tblAssesstempleMapper.findByCount(pageInfo);
        pageInfo.setTlist(list);
        pageInfo.setTotalRecord(count);
        return ResponseFormat.retParam(1, 200, pageInfo);

//        Page<TblAssesstemple> page = new Page<>();
//        page.setSize(pageSize);
//        page.setCurrent(pageNumber);
//
//        LambdaQueryWrapper<TblAssesstemple> wrapper = Wrappers.<TblAssesstemple>lambdaQuery();
//        if (null != tblAssesstemple.getTempleNumber() ){
//            wrapper.eq(TblAssesstemple::getTempleNumber, tblAssesstemple.getTempleNumber());
//        }
//        if (null != tblAssesstemple.getTemplename()){
//            wrapper.eq(TblAssesstemple::getTemplename, tblAssesstemple.getTemplename());
//        }
//        Page<TblAssesstemple> tblAssesstemplePage = tblAssesstempleMapper.selectPage(page, wrapper);
//        List<TblAssesstemple> assesstempleList = tblAssesstemplePage.getRecords();
//        return ResponseFormat.retParam(1, 200, assesstempleList);
    }

    @Override
    public JsonBean add(TblAssesstemple tblAssesstemple) {
        if (null == tblAssesstemple){
            return ResponseFormat.retParam(0, 10002, "缺少参数，请检查后重试");
        }

        tblAssesstempleMapper.insert(tblAssesstemple);

        return ResponseFormat.retParam(1, 200, "success");
    }

    @Override
    public JsonBean modify(TblAssesstemple tblAssesstemple) {

        tblAssesstempleMapper.updateByPrimaryKey(tblAssesstemple);

        return ResponseFormat.retParam(1, 200, "success");
    }

    @Override
    public JsonBean delete(BigDecimal asstemid) {
        if (null == asstemid){
            return ResponseFormat.retParam(0, 10002, "缺少参数，请检查重试");
        }

        TblAssesstemple dbAssesstemple = tblAssesstempleMapper.findById(asstemid);

        if (null == dbAssesstemple){
            return ResponseFormat.retParam(0, 50001, "删除的数据不存在，请检查后重试");
        }

        if (dbAssesstemple.getTblAssesses().size() > 0){
            return ResponseFormat.retParam(0, 30001, "改模板正在使用中不能删除");
        }

        tblAssesstempleMapper.deleteId(asstemid);

        return ResponseFormat.retParam(1, 200, "success");
    }

    @Override
    public TblAssesstemple findByid(BigDecimal tmplId) {
        return tblAssesstempleMapper.findById(tmplId);
    }

    @Override
    public List<TblAssesstemple> getTmplByNumber(String templeNumber, BigDecimal orgid) {
        return tblAssesstempleMapper.getTmplByNumber(templeNumber, orgid);
    }


}
